package com.sifang.POI.dianping;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;

public class AddCityCommand implements Command {
	
	private int cityId;

	public AddCityCommand(int cityId) {
		if (!isValidCityId(cityId))
			throw new IllegalArgumentException("invalid city id");
		this.cityId = cityId;
	}
	
	@Override
	public void execute() throws IOException, XPatherException {
		List<String> urls = getPreJobsUrls(cityId);
		Session s = DpDb.getInstance().openSession();
		s.beginTransaction();
		for (String url : urls) {
			String query = String.format("from PreJob preJob where preJob.url = '%s'", url);
			PreJob pj = (PreJob) s.createQuery(query).setMaxResults(1).uniqueResult();
			
			if (pj == null) {
				System.out.print("\radd " + url);
				pj = new PreJob(url, JobStatus.STATUS_NONE, 0);
				s.save(pj);
			}
		}
		s.getTransaction().commit();
		s.close();
	}

	private List<String> getPreJobsUrls(int cityId) throws IOException, XPatherException {
		TagNode node = WebUtil.cleanUrl(String.format("http://www.dianping.com/shopall/%d/0", cityId));
		
		ArrayList<String> categoryUrls = new ArrayList<String>();
		ArrayList<String> regionUrls = new ArrayList<String>();
		ArrayList<String> finalUrls = new ArrayList<String>();
		
		Object[] values = node.evaluateXPath("//div[@class='box shopallCate']");
		Object[] categoriesLv1 = ((TagNode) values[0]).evaluateXPath("//dl[@class='list']");
		for (Object nodeLv1 : categoriesLv1) {
			Object[] categoriesLv2 = ((TagNode) nodeLv1).evaluateXPath("//a[@class='B']");
			for (Object nodeLv2 : categoriesLv2) {
				categoryUrls.add(((TagNode) nodeLv2).getAttributeByName("href"));
			}
		}
		
		Object[] regionsLv1 = ((TagNode) values[1]).evaluateXPath("//dl[@class='list']");
		for (Object nodeLv1 : regionsLv1) {
			TagNode regionLv1 = (TagNode) ((TagNode) nodeLv1).evaluateXPath("//a[@class='Bravia']")[0];
			regionUrls.add(regionLv1.getAttributeByName("href"));
			Object[] regionsLv2 = ((TagNode) nodeLv1).evaluateXPath("//a[@class='B']");
			for (Object nodeLv2 : regionsLv2) {
				TagNode regionLv2 = (TagNode) nodeLv2;
				regionUrls.add(regionLv2.getAttributeByName("href"));
			}
		}
		
		for (String categoryUrl : categoryUrls) {
			StringBuilder sb = new StringBuilder("http://www.dianping.com").append(categoryUrl);
			for (String regionUrl : regionUrls) {
				String finalUrl = sb.toString() + regionUrl.substring(regionUrl.lastIndexOf('r'));
				finalUrls.add(finalUrl);
//				System.out.println(finalUrl);
			}
		}
		
		return finalUrls;
	}

	private boolean isValidCityId(int cityId) {
		return true;
	}

	/**
	 * @param args
	 * @throws XPatherException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException, XPatherException {
		new AddCityCommand(288).execute();
	}

}
